package net.expectx.pay.merchant.tag;



import net.expectx.pay.dao.bean.SystemType;
import net.expectx.pay.dao.bean.SystemTypeGroup;
import net.expectx.pay.dao.bean.SystemTypeGroupExample;
import net.expectx.pay.rpc.api.SystemTypeGroupService;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import javax.servlet.jsp.JspWriter;
import java.util.List;


/**
 * @author jianhun
 * @date 2018/5/30
 */
public class DictSelectTag extends RequestContextAwareTag {
    private static final String SELECT = "select";
    private static final String RADIO = "radio";
    private static final String CHECK_BOX = "checkbox";

    /**
     * 控件唯一标志着
     */
    /**
     * 控件名称
     * 必填
     */
    private String field;
    /**
     * 字典分组编码
     * 必填
     */
    private String typeGroupCode;
    /**
     * 控件类型 select|radio|checkbox  默认select
     * 选填
     */
    private String type;
    /**
     * 默认值
     * 选填
     */
    private String defaultVal;

    /**
     * 是否验证  默认true
     * 选填
     */
    private Boolean verify;

    private String placeholder;

    private SystemTypeGroupService systemTypeGroupService;

    @Override
    protected int doStartTagInternal() throws Exception {

        systemTypeGroupService = (SystemTypeGroupService) this.getRequestContext()
                .getWebApplicationContext().getBean(SystemTypeGroupService.class);
        SystemTypeGroupExample systemTypeGroupExample=new SystemTypeGroupExample();
        systemTypeGroupExample.createCriteria().andTypeGroupCodeEqualTo(typeGroupCode);
        systemTypeGroupExample.setOrderByClause("gmt_modified desc");
        List<SystemTypeGroup> typeGroups = systemTypeGroupService.selectByExample(systemTypeGroupExample);
        StringBuffer buffer = new StringBuffer();
        if (null == typeGroups || typeGroups.isEmpty()) {
            buffer.append(errorTag("无效的字典编号"));
        } else {
            SystemTypeGroup typeGroup = typeGroups.get(0);
            if (typeGroup.getIsDeleted() == 0) {
                List<SystemType> types = typeGroup.getSystemTypes();
                if (null == type) {
                    type = SELECT;
                }
                switch (type) {
                    case SELECT:
                        buffer.append(select(types));
                        break;
                    case RADIO:
                        buffer.append(radio(types));
                        break;
                    case CHECK_BOX:
                        buffer.append(checkBox(types));
                        break;
                    default:
                        buffer.append(errorTag("无效的表单元素类型,仅支持[select|radio|checkbox]"));
                }
            }
        }
        JspWriter writer = this.pageContext.getOut();
        try {
            writer.println(buffer.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.doEndTag();

    }

    /**
     * 下拉列表
     *
     * @param types
     * @return
     */
    public String select(List<SystemType> types) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<select id=\"" + field + "\" lay-filter=\"" + field + "\" name=\"" + field + "\" ");
        if (null == verify || verify) {
            buffer.append(" lay-verify=\"required\" lay-verType=\"tips\"");
        }
        buffer.append(">");
        if (null ==types || types.isEmpty()) {
            buffer.append("<optgroup label=\"暂无数据项\"></optgroup>");
        } else {
            if (null == placeholder){
                buffer.append("<option value=\"\">请选择</option>");
            }else{
                buffer.append("<option value=\"\">"+placeholder+"</option>");

            }
            for (SystemType systemType : types) {
                if (null != defaultVal) {
                    if (defaultVal.equals(systemType.getTypeCode())) {
                        buffer.append("<option   selected=\"selected\" value=\"" + systemType.getTypeCode() + "\">" + systemType.getTypeName() + "</option>");
                    } else {
                        buffer.append("<option  value=\"" + systemType.getTypeCode() + "\">" +systemType.getTypeName() + "</option>");
                    }
                } else {
                    buffer.append("<option value=\"" + systemType.getTypeCode() + "\">" + systemType.getTypeName()+ "</option>");
                }
            }
        }
        buffer.append("</select>");
        return buffer.toString();
    }

    /**
     * 单选按钮
     *
     * @param types
     * @return
     */
    public String radio(List<SystemType> types) {
        StringBuffer buffer = new StringBuffer();
        if (types.isEmpty()) {
            buffer.append(errorTag("无效的字典编号"));
        } else {
            buffer.append("<div class=\"layui-input-block\">");
            for (SystemType systemType : types) {
                String checked = "";
                if (null != defaultVal) {
                    if (defaultVal.equals(systemType.getTypeCode())) {
                        checked = "checked";
                    }
                }
                buffer.append(" <input type=\"radio\" name=\"" + field + "\"  lay-filter=" + field + " id=\"" + field + "_" + systemType.getTypeCode() + "\"   value=\"" + systemType.getTypeCode() + "\" " + checked + "  title=\"" + systemType.getTypeName()+ "\"> ");
            }
            buffer.append("</div>");
        }
        return buffer.toString();
    }

    /**
     * 复选框
     *
     * @param types
     * @return
     */
    public String checkBox(List<SystemType> types) {
        StringBuffer buffer = new StringBuffer();
        if (types.isEmpty()) {
            buffer.append(errorTag("无效的字典编号"));
        } else {
            buffer.append("<div class=\"layui-input-block\">");
            for (SystemType systemType : types) {
                buffer.append("<label class=\"am-checkbox-inline\">");
                String checked = "";
                if (null != defaultVal) {
                    String[] defaultVals = defaultVal.split(",");
                    for (String value : defaultVals) {
                        if (value.equals(systemType.getTypeCode())) {
                            checked = "checked";
                            continue;
                        }
                    }
                }
                buffer.append(" <input type=\"checkbox\" name=\"" + field + "[" + systemType.getTypeCode() + "]\" value=\"" + systemType.getTypeCode() + "\"  " + checked + " title=\"" + systemType.getTypeName() + "\"> ");
            }
            buffer.append("</div>");
        }
        return buffer.toString();
    }

    public String errorTag(String msg) {

        return "<div style='color:red'>" + msg + "</div>";

    }

    public String getField() {

        return field;

    }

    public void setField(String field) {

        this.field = field;

    }

    public String getTypeGroupCode() {

        return typeGroupCode;

    }

    public void setTypeGroupCode(String typeGroupCode) {

        this.typeGroupCode = typeGroupCode;

    }

    public String getType() {

        return type;

    }

    public void setType(String type) {

        this.type = type;

    }

    public String getDefaultVal() {

        return defaultVal;

    }

    public void setDefaultVal(String defaultVal) {

        this.defaultVal = defaultVal;

    }

    public Boolean getVerify() {
        return verify;
    }

    public void setVerify(Boolean verify) {
        this.verify = verify;
    }

    public String getPlaceholder() {
        return placeholder;
    }

    public void setPlaceholder(String placeholder) {
        this.placeholder = placeholder;
    }
}
